﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;

namespace QldtSdh.BLL
{
    public partial class BizKhoaHoc
    {
        /// <summary>
        /// Lấy danh sách
        /// </summary>
        /// <returns></returns>
        public static List<KhoaHoc> GetList(CEMS4UG_DHKHContext dbContext = null)
        {
            if (dbContext != null)
                return dbContext.KhoaHocDbSet.OrderByDescending(e => e.NamTuyenSinh).ThenBy(e => e.TenKhoaHoc).ToList();
            else
                using (var db = CEMS4UG_DHKHContext.GetContext())
                {
                    return db.KhoaHocDbSet.OrderByDescending(e => e.NamTuyenSinh).ThenBy(e => e.TenKhoaHoc).ToList();
                }
        }

        public static KhoaHoc GetByID(string maKhoaHocGUID)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.KhoaHocDbSet.Where(e => e.MaKhoaHocGUID == maKhoaHocGUID).FirstOrDefault();
            }
        }

        public static bool IsExistByID(string maKhoaHocGUID)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.KhoaHocDbSet.Where(e => e.MaKhoaHocGUID == maKhoaHocGUID).FirstOrDefault() == null ? false : true;
            }
        }

        public static CODE_RESULT_RETURN Add(KhoaHoc obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var khoaHoc = db.KhoaHocDbSet.Where(e => e.TenKhoaHoc == obj.TenKhoaHoc || e.NamTuyenSinh == obj.NamTuyenSinh).FirstOrDefault();
                if (khoaHoc != null)
                    return CODE_RESULT_RETURN.TenTrung;
                obj.MaKhoaHocGUID = Guid.NewGuid().ToString();
                db.KhoaHocDbSet.Add(obj);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<KhoaHoc>(nhatKy, null, obj.MaKhoaHocGUID, LOAIDOITUONG.KHÓA_HỌC, LOAITHAOTAC.Thêm);
                BizNhatKyHoatDong.Add<KhoaHoc>(nhatKy, obj);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Update(KhoaHoc obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _kh = db.KhoaHocDbSet.Where(e => (e.TenKhoaHoc == obj.TenKhoaHoc || e.NamTuyenSinh == obj.NamTuyenSinh) && e.MaKhoaHocGUID != obj.MaKhoaHocGUID).FirstOrDefault();
                if (_kh != null)
                    return CODE_RESULT_RETURN.TenTrung;
                _kh = db.KhoaHocDbSet.Where(e => e.MaKhoaHocGUID == obj.MaKhoaHocGUID).FirstOrDefault();
                if (_kh == null)
                    return CODE_RESULT_RETURN.ThatBai;
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<KhoaHoc>(nhatKy, _kh, obj.MaKhoaHocGUID, LOAIDOITUONG.KHÓA_HỌC, LOAITHAOTAC.Chỉnh_sửa);

                _kh.TenKhoaHoc = obj.TenKhoaHoc;
                _kh.NamTuyenSinh = obj.NamTuyenSinh;
                _kh.DangHoatDong = obj.DangHoatDong;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<KhoaHoc>(nhatKy, _kh);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Delete(KhoaHoc obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _kh = db.KhoaHocDbSet.Where(e => e.MaKhoaHocGUID == obj.MaKhoaHocGUID).FirstOrDefault();
                if (_kh == null)
                    return CODE_RESULT_RETURN.ThatBai;
                if (_kh.KhoaHoc_NganhDaoTao != null && _kh.KhoaHoc_NganhDaoTao.Count > 0)
                    return CODE_RESULT_RETURN.ThatBai;
                db.KhoaHocDbSet.Remove(_kh);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<KhoaHoc>(nhatKy, obj, obj.MaKhoaHocGUID, LOAIDOITUONG.KHÓA_HỌC, LOAITHAOTAC.Xóa);
                BizNhatKyHoatDong.Add<KhoaHoc>(nhatKy, null);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }

}
